package top.lemos.navib;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;


public class NaviActivity extends Activity {
    public static final String IS_ELEVATOR_CHECKED="is_elevator_check";
    public static final String BUILDING_NUM = "building_num";
    public static final String CLASSROOM_NUM = "classroom_num";
    private String mBuidingNum;
    private String mClassroomNum;
    private Boolean mIsChecked;
    private EditText mStartPoint;
    private EditText mEndPoint;
    private CheckBox mCheckBox;
    private Button mQueryButton;
    private ImageButton mHintButton;
    private ImageButton mExitutton;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_navi);
        mIsChecked=true;

        mStartPoint=(EditText)findViewById(R.id.start_point_text);
        mStartPoint.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                mBuidingNum=s.toString();
            }

            @Override
            public void afterTextChanged(Editable s) {
                if(!mBuidingNum.isEmpty()&&!Nav.GetInstance().IsDormValid(s.toString()))
                    Toast.makeText(getApplicationContext(),R.string.no_building, Toast.LENGTH_SHORT).show();
            }
        });
        mEndPoint=(EditText)findViewById(R.id.end_point_text);
        mEndPoint.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                mClassroomNum=s.toString();
            }

            @Override
            public void afterTextChanged(Editable s) {
                //判断是否合法，并提示
                if(mClassroomNum.length()==3&&!Nav.GetInstance().IsClassroomValid(s.toString()))
                    Toast.makeText(getApplicationContext(),R.string.no_classroom, Toast.LENGTH_SHORT).show();
            }
        });
        mCheckBox=(CheckBox)findViewById(R.id.elevator_checkbox);
        mCheckBox.setChecked(mIsChecked);
        mCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mIsChecked=isChecked;
            }
        });
        mQueryButton=(Button)findViewById(R.id.query_button);
        mQueryButton.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                if(mClassroomNum==null||mBuidingNum==null||mClassroomNum.isEmpty()||mBuidingNum.isEmpty()){
                    Toast.makeText(getApplicationContext(),R.string.nocompletely, Toast.LENGTH_SHORT).show();
                }
                else if(!Nav.GetInstance().IsClassroomValid(mClassroomNum)||
                        !Nav.GetInstance().IsDormValid(mBuidingNum)){
                    Toast.makeText(getApplicationContext(),R.string.invaild, Toast.LENGTH_SHORT).show();
                }else {
                    Intent i = new Intent(NaviActivity.this, ShowActivity.class);
                    i.putExtra(ShowActivity.EXTRA_BUILDING_NUM, mBuidingNum);
                    i.putExtra(ShowActivity.EXTRA_CLASSROOM_NUM, mClassroomNum);
                    i.putExtra(ShowActivity.EXTRA_IS_ELEVATOR_USED, mIsChecked);
                    startActivity(i);
                }
            }
        });
        mHintButton=(ImageButton)findViewById(R.id.hint_image);
        mHintButton.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                int messageReId=R.string.hint_toast;
                Toast.makeText(getApplicationContext(), messageReId, Toast.LENGTH_SHORT).show();
            }
        });
        mExitutton=(ImageButton)findViewById(R.id.exit_button);
        mExitutton.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                finish();
            }
        });


    }
}